import Vue from 'vue'
import Vuex from 'vuex'
import request from '../config/TokenRequst.js'
import util from "@/config/util.js"


//按时间push进chatList
// const timePush = (index, message) => {
// 	let left = 0
// 	let right = store.state.chatList[index].messageList.length - 1
// 	if (right < 0) {
// 		store.state.chatList[index].messageList.push(message)
// 		return
// 	}
// 	let i = new Date(store.state.chatList[index].messageList[right].sendDate).getTime()
// 	let x = new Date(message.sendDate).getTime()
// 	if (i < x) {
// 		store.state.chatList[index].messageList.push(message)
// 		return
// 	}
// 	while (left < right) {
// 		let mid = parseInt((left + right) / 2)
// 		i = new Date(store.state.chatList[index].messageList[mid].sendDate).getTime()
// 		if (i >= x) right = mid
// 		else left = mid + 1
// 	}
// 	store.state.chatList[index].messageList.splice(left, 0, message)
// }

Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		isLogin: false,
		socket: null,
		user: null,
		token: "",
		chatList: new Array(),
		singleChatMap: null,
		timer: 0
	},
	mutations: {
		init(state, socket) {
			state.socket = socket
			if (state.isLogin === true) return;
			let user = uni.getStorageSync("user")
			let token = uni.getStorageSync("token")
			if (user !== null && user !== undefined && user !== "") {
				state.user = user
				state.token = token
				state.isLogin = true
				state.singleChatMap = util.jsonToMap(uni.getStorageSync(state.user.uid + "singleChatMap"))
				state.chatList = uni.getStorageSync(state.user.uid + "chatList")
				this.commit("setTabBar")
				if (state.chatList === null || state.chatList === undefined || state.chatList === "") {
					state.chatList = new Array();
				}
				if (!socket.is_open_socket) socket.connectSocketInit();
			}
		},
		login(state, data) {
			let user = data.user
			let token = data.token
			state.user = user
			state.token = token
			uni.setStorageSync("user", user)
			uni.setStorageSync("token", token)
			state.isLogin = true
			state.singleChatMap = util.jsonToMap(uni.getStorageSync(state.user.uid + "singleChatMap"))
			state.chatList = uni.getStorageSync(state.user.uid + "chatList")
			this.commit("setTabBar")
			if (state.chatList === null || state.chatList === undefined || state.chatList === "") {
				state.chatList = new Array();
			}
		},
		updateUserInfo(state, user) {
			state.user = user;
			uni.setStorageSync("user", user)
		},
		logout(state) {
			state.socket.close()
			state.isLogin = false
			state.user = null
			uni.removeStorage({
				key: "user"
			})
			uni.removeStorage({
				key: "token"
			})
		},
		addMessage(state, message) {
			let to = message.to
			let from = message.from
			if (from === state.user.uid) {
				//TODO:接收的自己发送给服务器,返回的完整信息
				let index = util.findIdAndAdd(to)
				state.chatList[index].messageList.push(message)
				// timePush(index, message)
			} else {
				//TODO:接收的别人发送给我的信息
				let index = util.findIdAndAdd(from)
				// timePush(index, message)
				state.chatList[index].messageList.push(message)
				state.chatList[index].unRead += 1
			}
			this.commit("storeChatList")
			this.commit("setTabBar")
		},
		deleteChat(state, index) {
			state.singleChatMap.delete(parseInt(state.chatList[index].user.uid))
			for (var s of state.singleChatMap) {
				if (s[1] > index) {
					state.singleChatMap.set(parseInt(s[0]), parseInt(s[1] - 1))
				}
			}
			state.chatList.splice(index, 1)
			state.timer = new Date().getTime()
			this.commit("storeChatList")
			this.commit("setTabBar")
		},
		setTabBar(state) {
			let sum = 0
			for (let i = 0; i < state.chatList.length; i++) {
				sum += state.chatList[i].unRead;
			}
			if (sum > 0) {
				uni.setTabBarBadge({
					index: 2,
					text: sum.toString()
				})
			} else {
				uni.removeTabBarBadge({
					index: 2
				})
			}
		},
		clearUnread(state, index) {
			// let index = findId(friendId, state.chatList)
			state.chatList[index].unRead = 0
			this.commit("storeChatList")
			this.commit("setTabBar")
		},
		storeChatList(state) {
			uni.setStorage({
				key: state.user.uid + "chatList",
				data: state.chatList
			})
			uni.setStorage({
				key: state.user.uid + "singleChatMap",
				data: util.mapToJson(state.singleChatMap)
			})
		},
		storeUser(state) {
			uni.setStorage({
				key: "user",
				data: state.user
			})
		}
	},
})
export default store
